<template>
  <div>
    <el-form ref="formRef" label-width="80px" :model="form" :rules="formRules" style="width: 500px">
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="员工密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="updateAccount">更新</el-button>
        <el-button type="primary" size="mini" @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmpInfoAPI, updateEmpAPI } from '@/api/emp.js'
export default {
  name: 'EmpAccount',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      formRules: {
        username: [{ required: true, message: '员工姓名是必填', trigger: 'blur' }],
        password: [{ required: true, message: '员工密码是必填', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.initEmpInfo()
  },
  methods: {
    async initEmpInfo() {
      const result = await getEmpInfoAPI(this.$route.params.id)
      this.form.username = result.data.username
    },
    updateAccount() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // 调用接口
        await updateEmpAPI({ id: this.$route.params.id, ...this.form })
        // 提示用户
        this.$message.success('更新成功！')
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
